const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
// const imagesBaseUrl = 'http://124.220.3.30:9000/miniapp-xjy-dev/';
const imagesBaseUrl = 'http://124.220.3.30:9000/miniapp-xjy-dev/';
const baseUrl = 'https://api.nbyingchuang.cn:8443/';
const https = (url, data, successF, failF, completeF, jsonFlag) => {
  // console.log(data)
  data.channel = 'xjyjzfw';
  // console.log(url);
  // let baseUrl = 'http://192.168.110.4:8081/';
  // let baseUrl = 'https://api.nbyingchuang.cn:8443/';
  wx.request({
    url: baseUrl + url,
    data: jsonFlag ? JSON.stringify(data) : data,
    method: 'POST',
    header: {
      AccessToken: wx.getStorageSync('AccessToken')
    },
    success: (res) => {
      // console.log('https-success')
      // console.log(res)
      if (res.data.errorCode == 'access.token.invalid' || res.data.errorCode == 'access.token.required') {
        wx.setStorageSync('AccessToken', '')
        wx.navigateTo({ url: "/pages/login/index" });
      } else if (res.header && res.header.AccessToken) {
        wx.setStorageSync('AccessToken', res.header.AccessToken)
      }
      if (res.statusCode === 200) {
        if (res.data && res.data.returnCode === '000000') {
          successF(res.data)
        } else {
          failF(res.data)
        }
      } else if (res.statusCode !== 200) {
        failF({ errorMsg: '接口报错！' })
      }


    },
    fail: (res) => {
      console.log('https-fail')
      console.log(res)
      failF(res)
    },
    complete: (res) => {
      console.log('https-complete')
      console.log(res)
      if (completeF) { completeF() }
    }
  })
  // return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  https,
  imagesBaseUrl,
  baseUrl
}
